package com.srv.util;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
import java.util.Set;

public class SqlUtil {
	public static String generateWhereCause(Map<ObjectField, Object> keyValues) {
        StringBuffer returnValue = new StringBuffer();

        int columnNum = keyValues.size();
        int i = 1;

        for (ObjectField field : keyValues.keySet()) {
            returnValue.append(field.getColumn()).append(" = ? ");

            if (i < columnNum) {
                returnValue.append("AND ");
            }

            i++;
        }

        return returnValue.toString();
    }

    public static String generateSetCause(Map<ObjectField, Object> keyValues) {
        StringBuffer returnValue = new StringBuffer();

        int columnNum = keyValues.size();
        int i = 1;

        for (ObjectField field : keyValues.keySet()) {
            returnValue.append(field.getColumn()).append(" = ? ");

            if (i < columnNum) {
                returnValue.append(", ");
            }

            i++;
        }

        return returnValue.toString();
    }

    public static String generateValuesCause(Map<ObjectField, Object> keyValues) {
    	StringBuffer columBuff = new StringBuffer("(");
    	StringBuffer valueBuff = new StringBuffer("(");
    	int columnNum = keyValues.size();
    	int i = 1;
    	
    	for (ObjectField field : keyValues.keySet()) {
    		columBuff.append(field.getColumn());
    		valueBuff.append("?");
    		
    		if(i < columnNum){
    			columBuff.append(", ");
    			valueBuff.append(", ");
    		}
    		
    		i++;
    	}
    	
    	columBuff.append(")");
		valueBuff.append(")");
		
		return columBuff.append(" VALUES ").append(valueBuff).toString();
    }
    
    public static int setStmtParams(PreparedStatement stmt, final int startIdx, Map<ObjectField, Object> keyValues) throws SQLException {
        int i = 0;

        for (ObjectField field : keyValues.keySet()) {
            if (field.getType().equals(ObjectFieldDBType.LONG)) {
                stmt.setLong(startIdx + i, (Long) keyValues.get(field));
            } else if (field.getType().equals(ObjectFieldDBType.INT)) {
                stmt.setInt(startIdx + i, (Integer) keyValues.get(field));
            } else if (field.getType().equals(ObjectFieldDBType.BOOLEAN)) {
                stmt.setString(startIdx + i, (Boolean) keyValues.get(field) ? "1" : "0");
            } else if (field.getType().equals(ObjectFieldDBType.STRING)) {
                stmt.setString(startIdx + i, (String) keyValues.get(field));
            } else if (field.getType().equals(ObjectFieldDBType.TIMESTAMP)) {
                stmt.setTimestamp(startIdx + i, new Timestamp(((Date) keyValues.get(field)).getTime()));
            } else if (field.getType().equals(ObjectFieldDBType.DOUBLE)) {
                stmt.setDouble(startIdx + i, (Double)keyValues.get(field));
            } 

            i++;
        }

        return i + startIdx;
    }
    
    public static int setStmtParamsByKeyset(PreparedStatement stmt, final int startIdx, Set<ObjectField> keyset, Map<ObjectField, Object> keyValues) throws SQLException {
    	int i = 0;

        for (ObjectField field : keyset) {
            if (field.getType().equals(ObjectFieldDBType.LONG)) {
                stmt.setLong(startIdx + i, (Long) keyValues.get(field));
            } else if (field.getType().equals(ObjectFieldDBType.INT)) {
                stmt.setInt(startIdx + i, (Integer) keyValues.get(field));
            } else if (field.getType().equals(ObjectFieldDBType.BOOLEAN)) {
                stmt.setString(startIdx + i, (Boolean) keyValues.get(field) ? "1" : "0");
            } else if (field.getType().equals(ObjectFieldDBType.STRING)) {
                stmt.setString(startIdx + i, (String) keyValues.get(field));
            } else if (field.getType().equals(ObjectFieldDBType.TIMESTAMP)) {
                stmt.setTimestamp(startIdx + i, new Timestamp(((Date) keyValues.get(field)).getTime()));
            } else if (field.getType().equals(ObjectFieldDBType.DOUBLE)) {
                stmt.setDouble(startIdx + i, (Double)keyValues.get(field));
            } 

            i++;
        }

        return i + startIdx;
    }
}
